From 56a077dd3274ba8273946b8e60a095233286a43e Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Wed, 1 Oct 2014 08:53:05 +0200 Subject: [PATCH] Add --open flag to cargo doc --- src/bin/doc.rs | 2 ++ src/cargo/ops/cargo_doc.rs | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/bin/doc.rs b/src/bin/doc.rs index d06353598..601a284db 100644 --- a/src/bin/doc.rs +++ b/src/bin/doc.rs @@ -13,6 +13,7 @@ Usage: Options: -h, --help Print this message + --open Opens the docs in a browser after the operation --no-deps Don't build documentation for dependencies -j N, --jobs N The number of jobs to run in parallel --features FEATURES Space-separated list of features to also build @@ -33,6 +34,7 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult let mut doc_opts = ops::DocOptions { all: !options.flag_no_deps, + open_result: options.flag_open, compile_opts: ops::CompileOptions { env: if options.flag_no_deps {"doc"} else {"doc-all"}, shell: shell, diff --git a/src/cargo/ops/cargo_doc.rs b/src/cargo/ops/cargo_doc.rs index 486b5e46f..2a34f7fd4 100644 --- a/src/cargo/ops/cargo_doc.rs +++ b/src/cargo/ops/cargo_doc.rs @@ -3,10 +3,12 @@ use std::collections::HashSet; use core::source::Source; use ops; use sources::PathSource; +use std::io::process::Command; use util::{CargoResult, human}; pub struct DocOptions<'a> { pub all: bool, + pub open_result: bool, pub compile_opts: ops::CompileOptions<'a>, } @@ -34,5 +36,54 @@ pub fn doc(manifest_path: &Path, } try!(ops::compile(manifest_path, &mut options.compile_opts)); + + if options.open_result { + use std::io::fs::PathExtensions; + + match lib_names.iter().nth(0).map(|l| package.get_absolute_target_dir() + .join("doc").join(*l).join("index.html")) + { + Some(ref path) if path.exists() => open_docs(path), + _ => () + } + } + Ok(()) } + +#[cfg(not(any(target_os = "windows", target_os = "macos")))] +fn open_docs(path: &Path) { + // trying xdg-open + match Command::new("xdg-open").arg(path).detached().status() { + Ok(_) => return, + Err(_) => () + }; + + // trying gnome-open + match Command::new("gnome-open").arg(path).detached().status() { + Ok(_) => return, + Err(_) => () + }; + + // trying kde-open + match Command::new("kde-open").arg(path).detached().status() { + Ok(_) => return, + Err(_) => () + }; +} + +#[cfg(target_os = "windows")] +fn open_docs(path: &Path) { + match Command::new("start").arg(path).detached().status() { + Ok(_) => return, + Err(_) => () + }; +} + +#[cfg(target_os = "macos")] +fn open_docs(path: &Path) { + match Command::new("open").arg(path).detached().status() { + Ok(_) => return, + Err(_) => () + }; +} -- 2.30.2